package com.xdtech.hotel.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.CardDao;
import com.xdtech.hotel.model.Card;
import com.xdtech.hotel.model.CardToRoom;
import com.xdtech.hotel.protocol.manager.OperationManager;
import com.xdtech.hotel.service.CardService;
import com.xdtech.hotel.vo.CardItem;
import com.xdtech.hotel.vo.RoomItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:11:08
 * @since 1.0
 * @see
 */
@Service
public class CardServiceImpl implements CardService {
	@Autowired
	private CardDao cardDao;
	@Autowired
	private BaseDao baseDao;

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @param entity
	 */
	public void save(Card entity) {
		cardDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<Card> entities) {
		cardDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @param entity
	 */
	public void delete(Card entity) {
		cardDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		cardDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @param id
	 * @return
	 */
	public Card get(Long id) {
		return cardDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @return
	 */
	public List<Card> getAll() {
		return cardDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> cards = null;
		long rows = 0;
		if (pg!=null) {
			Page<Card> page = cardDao.findPage(pg,"from Card order by createTime desc", values);
			cards = BeanUtils.copyListProperties(
					CardItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<Card> cardList = cardDao.find("from Card order by id desc", values);
			cards = BeanUtils.copyListProperties(
					CardItem.class, cardList);
			rows = cards.size();
		}
		results.put("total", rows);
		results.put("rows", cards);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateCard(CardItem item) {
		Card card = null;
		if (item.getId()==null) {
			card = new Card();
		}else {
			card = cardDao.get(item.getId());
		}
		//澶嶅埗鍓嶅彴淇敼鐨勫睘鎬�
		BeanUtils.copyProperties(card, item);
		cardDao.save(card);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @param cardId
	 * @return
	 */
	public CardItem loadCardItem(Long cardId) {
		Card card = cardDao.get(cardId);
		CardItem cardItem = new CardItem();
		BeanUtils.copyProperties(cardItem, card);
		return cardItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteCardInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-04 11:11:08
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteCardInfo(List<Long> cardIds) {
		for (Long id : cardIds) {
			delete(id);
		}
		return true;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-18下午12:16:33
	 * @modified by
	 * @param pg
	 * @param baseItem
	 * @param queryName
	 * @return
	 */
	@Override
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/* (非 Javadoc)
	 * getCardByNo
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param cardNo
	 * @return
	 * @see com.xdtech.hotel.service.CardService#getCardByNo(java.lang.String)
	 */
	@Override
	public Card getCardByNo(String cardNo)
	{
		return cardDao.findUnique("from Card where cardNo=?", cardNo);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-23下午9:34:53
	 * @modified by
	 * @param cardId
	 * @param rooms
	 */
	@Override
	public void saveCardToRooms(Long cardId, List<RoomItem> rooms) {
		Card card = get(cardId);
		//先清空
		cardDao.deleteRoomLinkCard(cardId);
		//再保存
		List<CardToRoom> cardToRooms = new ArrayList<CardToRoom>();
		for (RoomItem roomItem : rooms) {
			cardToRooms.add(new CardToRoom(card.getCardNo(),card.getId(),roomItem.getId()));
		}
		baseDao.saveAll(cardToRooms);
		OperationManager.asyncWriteCards(card.getCardType(), card.getCardNo(), rooms);
	}

}
